package com.pf.java.File;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.Arrays;

/**
 *  过滤文件
 *
 * ClassName:IO2
 * Package:com.pf.java.File
 *
 * @author: pf
 * @date: 2021/11/5-21:17
 */
public class IO2 {

    public static void main(String[] args) {
        File dir = new File("C:/Users/10970/Desktop/Java学习笔记/牛客网学习");

        File[] files = dir.listFiles();

        // 实现FileFilter接口
        files = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
//                System.out.println(pathname);
                if (pathname.getName().endsWith(".txt")) {
                    return true;
                }
                return false;
            }
        });

        // 实现FilenameFilter接口
        files = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
//                System.out.println(dir.getName() + "," + name);
                if (name.endsWith(".md")) {
                    return true;
                }
                return false;
            }
        });

        System.out.println(Arrays.toString(files));
    }

}
